package com.gouwudai.gobbs.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.dao.ITopicIlikeDAO;
import com.gouwudai.gobbs.pojo.TbbsTopicIlike;
import com.litt.core.exception.DBException;

public class TopicIlikeDAOImpl extends BaseDAOImpl implements ITopicIlikeDAO 
{
	private static final Logger logger = Logger.getLogger(TopicIlikeDAOImpl.class);	
	private static final String BUSI_NAME = "我订阅的主题";
	
	public void save(TbbsTopicIlike tbbsTopicIlike)
	{    		
		super.saveObject(tbbsTopicIlike);		
	}
	
	public void update(TbbsTopicIlike tbbsTopicIlike)
	{    		
		super.updateObject(tbbsTopicIlike);		
	}	
	
	public TbbsTopicIlike load(Integer id)
	{
		return (TbbsTopicIlike)super.loadObject(TbbsTopicIlike.class, id);
	} 
	
	public TbbsTopicIlike load(int topicId,int userId)
	{
		String hql = "from TbbsTopicIlike where topicId="+topicId+" and userId="+userId;
		return (TbbsTopicIlike)super.uniqueResultHql(hql);
	}
	
	
    public void delete(String ids)
    {
        super.deleteObjects(TbbsTopicIlike.class.getName(),"likeId",ids);
    } 

    /**
     * 查询我订阅的话题
     * @param userId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws DBException
     */
    public List listByUserId(int userId,int pageIndex,int pageSize)
    {
    	int fetchSize = pageIndex*pageSize;
    	StringBuilder listSql = new StringBuilder(200);
    	listSql.append("SELECT TOP ");
    	listSql.append(fetchSize);
    	listSql.append(" A.*,B.TOPIC_PRO,B.TOPIC_STYLE_LIST,B.TOPIC_IS_DEL,B.TITLE,B.TOPIC_LAST_RE_TIME,B.LOGIN_ID,B.CLASS_ID,C.CLASS_NAME FROM TbbsTopicIlike A,TbbsTopic B,TbbsClass C WHERE A.USER_ID=");
    	listSql.append(userId);
    	listSql.append(" AND A.TOPIC_ID=B.TOPIC_ID AND B.CLASS_ID=C.CLASS_ID");
    	List rsList = super.listSqlPage(listSql.toString(),pageIndex,pageSize).getRsList();
    	return rsList;
    }
    
    /**
     * 分页统计
     * @param userId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws DBException
     */
    public int listCountByUserId(int userId)
    {
    	StringBuilder listSql = new StringBuilder(200);
    	listSql.append("SELECT COUNT(*) FROM TbbsTopicIlike A,TbbsTopic B,TbbsClass C WHERE A.USER_ID=");
    	listSql.append(userId);
    	listSql.append(" AND A.TOPIC_ID=B.TOPIC_ID AND B.CLASS_ID=C.CLASS_ID");
    	int count = super.listSqlCount(listSql.toString());
    	return count;
    }
}
